package com.wman;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;

import com.blankj.utilcode.util.StringUtils;
import com.wman.sheep.common.utils.DensityUtil;

import androidx.appcompat.widget.AppCompatEditText;

/**
 * @ProjectName: wman
 * @Package: com.wman
 * @ClassName: MaterialEditText
 * @Description: java类作用描述
 * @Author: wman
 * @CreateDate: 2019/11/11 16:41
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/11 16:41
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MaterialEditText extends AppCompatEditText {
    private final static int TEXT_SIZE = DensityUtil.dip2px(12);
    private final static int TEXT_MARGIN = DensityUtil.dip2px(8);
    private final static int TEXT_HORIZONTAL_OFFSET = DensityUtil.dip2px(5);
    private final static int TEXT_VERTICAL_OFFSET = DensityUtil.dip2px(22);
    private final static int TEXT_ANIMATION_OFFSET = DensityUtil.dip2px(16);

    boolean flotingLableShown;
    boolean useFloatingLable;
    float floatingLableFraction;
    ObjectAnimator animator;
    Rect backgroundPadding = new Rect();

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public MaterialEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MaterialEditText);
        useFloatingLable = typedArray.getBoolean(R.styleable.MaterialEditText_useFloatingLable, true);
        typedArray.recycle();

        onUseFloatingLableChange();
        mPaint.setTextSize(TEXT_SIZE);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (useFloatingLable) {
                    if (flotingLableShown && StringUtils.isEmpty(getText().toString())) {
                        flotingLableShown = false;
                        getAnimator().reverse();
                    } else if (!flotingLableShown && !StringUtils.isEmpty(getText().toString())) {
                        flotingLableShown = true;
                        getAnimator().start();
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }


    private void onUseFloatingLableChange() {
        getBackground().getPadding(backgroundPadding);
        if (useFloatingLable) {
            setPadding(getPaddingLeft(), backgroundPadding.top + TEXT_SIZE + TEXT_MARGIN, getPaddingRight(), getPaddingBottom());
        } else {
            setPadding(getPaddingLeft(), backgroundPadding.top, getPaddingRight(), getPaddingBottom());
        }
    }

    public void setUseFloatingLable(boolean useFloatingLable) {
        if (this.useFloatingLable != useFloatingLable) {
            this.useFloatingLable = useFloatingLable;
            onUseFloatingLableChange();
        }
    }

    private ObjectAnimator getAnimator() {
        if (animator == null) {
            animator = ObjectAnimator.ofFloat(MaterialEditText.this, "floatingLableFraction", 0, 1);
        }
        return animator;
    }


    public float getFloatingLableFraction() {
        return floatingLableFraction;
    }

    public void setFloatingLableFraction(float floatingLableFraction) {
        this.floatingLableFraction = floatingLableFraction;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setAlpha((int) (floatingLableFraction * 0xff));
        float extraOffset = TEXT_ANIMATION_OFFSET * (1 - floatingLableFraction);
        canvas.drawText(getHint().toString(), TEXT_HORIZONTAL_OFFSET, TEXT_VERTICAL_OFFSET + extraOffset, mPaint);
    }
}
